﻿function area_select(idPrefix,defaultValArr) {
  var idPrefix=idPrefix||'', defaultValArr=defaultValArr||['中国','广东','深圳'];
  var countryDivId=idPrefix+"country",stateDivId=idPrefix+"province",cityDivId=idPrefix+"city";  
  var countryDefault=defaultValArr[0],stateDefault=defaultValArr[1],cityDefault=defaultValArr[2]; 
  var countryList=gj.Location.CountryRegion;
        //注意事项
        //有的国家是没有state的只有city,有的没有state
        //2. 获取select，初始化
  var countrySelectObj = document.getElementById(countryDivId);
  var stateSelectObj = document.getElementById(stateDivId);
  var citySelectObj = document.getElementById(cityDivId);
  countrySelectObj.add(new Option("--请选择--", ""));
  stateSelectObj.add(new Option("--请选择--", ""));
  citySelectObj.add(new Option("--请选择--", ""));
  var xmlCountryNodes = countryList;
  for (var i = 0; i < xmlCountryNodes.length; i++) {
    var xmlCountryName = xmlCountryNodes[i]['-Name'];
    var xmlCountryCode = xmlCountryNodes[i]['-Code'];
    countrySelectObj.add(new Option(xmlCountryName, xmlCountryCode))
  }
  $($('#'+countryDivId).find("option")).each(function () {
    if ($(this).text() == countryDefault) {
      $(this).attr("selected","selected")
    }
  });
  var index1=countrySelectObj.selectedIndex-1 ;
  if(index1<0){}else{
    var xmlStateNodes = countryList[index1]['State'];
    for (var j = 0; j < xmlStateNodes.length; j++) {
      var xmlStateName = xmlStateNodes[j]['-Name'];
      var xmlStateCode = xmlStateNodes[j]['-Code'];
      stateSelectObj.add(new Option(xmlStateName, xmlStateCode));
      citySelectObj.length = 0
      citySelectObj.add(new Option("--请选择--", ""))
    }
    $($('#'+stateDivId).find("option")).each(function () {
      if ($(this).text() == stateDefault) {
        $(this).attr("selected","selected")
      }
    });   
  }

  var index2=stateSelectObj.selectedIndex-1 ;
  if(index2<0){}else{
    var xmlCityNodes = xmlStateNodes[index2]['City'];
    for (var k = 0; k < xmlCityNodes.length; k++) {
      var xmlCityName = xmlCityNodes[k]['-Name'];
      var xmlCityCode = xmlCityNodes[k]['-Code'];
      citySelectObj.add(new Option(xmlCityName, xmlCityCode))
    }
    $($('#'+cityDivId).find("option")).each(function () {
      if ($(this).text() == cityDefault) {
        $(this).attr("selected","selected")
      }
    });
  }
  countrySelectObj.onchange = function() {
    stateSelectObj.length = 0;
    stateSelectObj.add(new Option("--请选择--", ""));
    if (this.value != "") {
      for (var i = 0; i < countryList.length; i++) {
        var xmlCountryCode = countryList[i]['-Code'];
        if (this.value == xmlCountryCode) {
          if (countryList[i]['State']) {
            var xmlStateNodes = countryList[i]['State']
          } else {
            var xmlStateNodes = {}
          }
          if (typeof xmlStateNodes === 'object' && !isNaN(xmlStateNodes.length)) {
            var fStateNodeName = xmlStateNodes[0]['-Name']
          } else {
            var fStateNodeName = xmlStateNodes['-Name']
          }
          if (fStateNodeName == null) {
            stateSelectObj.length = 0;
            stateSelectObj.add(new Option("--", ""));
            if (xmlStateNodes['City']) {
              var xmlCityNodes = xmlStateNodes['City']
            } else {
              var xmlCityNodes = {}
            }
            if (typeof xmlStateNodes === 'object' && !isNaN(xmlStateNodes.length)) {
              var fStateNodeName = xmlStateNodes[0]['-Name']
            } else {
              var fStateNodeName = xmlStateNodes['-Name']
            }
            citySelectObj.length = 0;
                      if(xmlCityNodes.length>0){
                          for (var l = 0; l < xmlCityNodes.length; l++) {
                              var xmlcityName = xmlCityNodes[l]['-Name'];
                              var xmlcityCode = xmlCityNodes[l]['-Code'];
                              citySelectObj.add(new Option(xmlcityName, xmlcityCode))
                          }
                      }else{
                          citySelectObj.add(new Option("--", ""))
                      }
                      
          } else {
            for (var j = 0; j < xmlStateNodes.length; j++) {
              var xmlStateName = xmlStateNodes[j]['-Name'];
              var xmlStateCode = xmlStateNodes[j]['-Code'];
              stateSelectObj.add(new Option(xmlStateName, xmlStateCode));
              citySelectObj.length = 0
                          citySelectObj.add(new Option("--请选择--", ""))
            }
          }
          break
        }
      }
    }
  }
  stateSelectObj.onchange = function() {
    citySelectObj.length = 0;
    citySelectObj.add(new Option("--请选择--", ""));
    if (this.value != "") {
      for (var i = 0; i < xmlCountryNodes.length; i++) {
        var xmlCountryCode = xmlCountryNodes[i]['-Code'];
        if (countrySelectObj.value == xmlCountryCode) {
          var xmlStateNodes = xmlCountryNodes[i]['State'];
          for (var j = 0; j < xmlStateNodes.length; j++) {
            if (stateSelectObj.value == xmlStateNodes[j]['-Code']) {
              var xmlCityNodes = xmlStateNodes[j]['City'];
              for (var k = 0; k < xmlCityNodes.length; k++) {
                var xmlCityName = xmlCityNodes[k]['-Name'];
                var xmlCityCode = xmlCityNodes[k]['-Code'];
                citySelectObj.add(new Option(xmlCityName, xmlCityCode))
              }
            }
          }
          break
        }
      }
    }
  }
}